// pages/ecergency/ecergencydetail/ecergencydetail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    eventId: 0,
    execContent: '',
    helpInfo: '',
    feeds: [],
    checked: false,
    attentionItem: '',
    contentType: 0,
    src: '',
    emgHandlePublicGoodsList: [],
    result: [],
    dis: false,
    hiddenmodalput: true,
    emgGoods: [],
    beizhu_input: '',
    id: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var eventId1 = options.eventId;
    console.log(1);
    that.setData({
      eventId: eventId1
    });
    that.getFeeds();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },



  getFeeds: function () {
    var that = this;
    var header;
    header = {
      'Cookie': wx.getStorageSync("sessionid"), //读取cookie
      'content-type': 'application/json'
    };
    var params = {
      eventId: that.data.eventId
    }
    wx.request({
      url: app.globalData.baseUrl + '/safe/emgHandlePublic/find',
      // data: '',
      header: header,
      data: params,
      method: 'post',
      // dataType: 'json',
      // responseType: 'text',
      success: function (res) {
        var feeds1 = [];
        var attentionItem = '';
        var eventLevel = '';
        var id = 0;
        var emgHandlePublicGoodsList = [];
        for (var item in res.data.data.list) {
          feeds1 = Object.assign(feeds1, res.data.data.list[item].emgHandleListPublics);
          attentionItem = res.data.data.list[item].attentionItem + attentionItem;
          emgHandlePublicGoodsList = Object.assign(emgHandlePublicGoodsList, res.data.data.list[item].emgHandlePublicGoodsList);
          eventLevel = res.data.data.list[item].eventLevel;
          id = res.data.data.list[item].id;
        }
        // console.log(attentionItem);
        // console.log(feeds1);
        var contentType = parseInt(eventLevel[0]);
        console.log(contentType);
        console.log(id);
        that.setData({
          feeds: feeds1,
          attentionItem: attentionItem,
          contentType: contentType,
          id: id,
          emgHandlePublicGoodsList: emgHandlePublicGoodsList
        })
        console.log(that.data.emgHandlePublicGoodsList);
      },
      fail: function (res) {
      },
      complete: function (res) {
      },
      // },
    })

  },
  onChange: function (event) {
    var that = this;
    that.setData({
      result: event.detail,
    });
    console.log(that.data.result);
    console.log(that.data.id);
    var header;
    header = {
      'Cookie': wx.getStorageSync("sessionid"), //读取cookie
      'content-type': 'application/json'
    };
    var params = {
      id: that.data.result[that.data.result.length - 1],
      handlePublicId: that.data.id,
      status: 1
    };

    wx.request({
      url: app.globalData.baseUrl + '/safe/emgHandleListPublic/update',
      // data: '',
      header: header,
      data: params,
      method: 'post',
      // dataType: 'json',
      // responseType: 'text',
      success: function (res) {

      },
      fail: function (res) {
      },
      complete: function (res) {
      },
      // },
    })

  },

  chooseImageTap: function () {
    var that = this;
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // var successUp = 0; //成功个数
        // var failUp = 0; //失败个数
        // var length = res.tempFilePaths.length; //总共个数
        // var i = 0; //第几个
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          src: tempFilePaths
        })
        // upload(tempFilePaths, successUp, failUp, i, length,that);
      }
    })
  },

  tapdetail: function (e) {
    var abc = e.currentTarget.dataset.abc;
    var that = this;
    that.setData({
      hiddenmodalput: !that.data.hiddenmodalput
    });
    var header;
    header = {
      'Cookie': wx.getStorageSync("sessionid"), //读取cookie
      'content-type': 'application/json'
    };
    var params = {
      goodsName: abc
    }

    wx.request({
      url: app.globalData.baseUrl + '/safe/emgGoods/find',
      // data: '',
      header: header,
      data: params,
      method: 'get',
      // dataType: 'json',
      // responseType: 'text',
      success: function (res) {
        console.log(res.data.data.list);
        that.setData({
          emgGoods: res.data.data.list
        })
      },
      fail: function (res) {
      },
      complete: function (res) {
      },
      // },
    })

  },

  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },
  //确认
  confirm: function () {
    this.setData({
      hiddenmodalput: true
    })
  },

  bindinput: function (e) {
    this.setData({
      beizhu_input: e.detail.value
    });
    console.log(this.data.beizhu_input);

  },

  savebeizhu: function () {
    var that = this;
    var header;
    header = {
      'Cookie': wx.getStorageSync("sessionid"), //读取cookie
      'content-type': 'application/json'
    };
    var params = {
      remark: that.data.beizhu_input,
      id: that.data.id
    }

    wx.request({
      url: app.globalData.baseUrl + '/safe/emgHandlePublic/update',
      // data: '',
      header: header,
      data: params,
      method: 'post',

      success: function (res) {

        var successUp = 0; //成功个数
        var failUp = 0; //失败个数
        var tempFilePaths = that.data.src;
        var length = tempFilePaths.length; //总共个数
        var i = 0; //第几个
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          upload(tempFilePaths, successUp, failUp, i, length, that);

      },
      fail: function (res) {
      },
      complete: function (res) {
      },
      // },
    })
  },

  deleteImage: function (event) {
    let index = event.currentTarget.dataset.idx;
    let tempArr = this.data.src;
    tempArr.splice(index, 1);
    this.setData({
      src: tempArr
    })
  },
  handleImagePreview(e) {
    const idx = e.target.dataset.idx
    const images = this.data.src
    wx.previewImage({
      current: images[idx],  //当前预览的图片
      urls: images,  //所有要预览的图片
    })
  },

  finishshijian:function(){
    var that = this;
    var header;
    header = {
      'Cookie': wx.getStorageSync("sessionid"), //读取cookie
      'content-type': 'application/json'
    };
    var params = {
      id: that.data.eventId,
      status:'1'
    }
    console.log(that.data.eventId);

    wx.request({
      url: app.globalData.baseUrl + '/safe/emgEvent/update',
      // data: '',
      header: header,
      data: params,
      method: 'post',

      success: function (res) {
      },
      fail: function (res) {
      },
      complete: function (res) {
      },
      // },
    })
  }
})

function upload(path, successUp, failUp, i, length, page) {
  var header;
  header = {
    'Cookie': wx.getStorageSync("sessionid"), //读取cookie
    "Content-Type": "multipart/form-data"
  };
  var contentType1 = page.data.contentType;
  var id1 = page.data.id;
  console.log(contentType1);
  console.log(id1);
  var params = {
    contentType: contentType1,
    id: id1
  }
  wx.showToast({
    icon: "loading",
    title: "正在上传"
  });
  if(length!=0){
    wx.uploadFile({
      url: app.globalData.baseUrl + "/safe/file/upload",
      method: 'post',
      header: header,
      // data: params,
      filePath: path[i],
      name: 'file',
      formData: {
        //和服务器约定的token, 一般也可以放在header中
        // 'session_token': wx.getStorageSync('session_token')
        contentType: contentType1,
        contentId: id1
      },
      success: function (res) {
        successUp++;
        console.log("上传成功：" + successUp);
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: '上传文件失败',
            showCancel: false
          })
          return;
        } else {
          wx.showModal({
            title: '提示',
            content: '上传文件成功',
            showCancel: false
          })
        }
        var data = res.data
        page.setData({  //上传成功修改显示头像
          src: Object.assign(path, page.data.src),
        })

        wx.navigateTo({
          url: '/pages/index/index'
        })
      },
      fail: function (e) {
        failUp++;
        console.log(failUp);
        wx.showModal({
          title: '提示',
          content: '上传文件失败',
          showCancel: false
        })
        return
      },
      complete: function () {
        i++;
        if (i == length) {
          wx.showToast('总共' + successUp + '张上传成功,' + failUp + '张上传失败！');
        }
        else {  //递归调用uploadDIY函数
          upload(path, successUp, failUp, i, length, page);
        }
        // wx.hideToast();  //隐藏Toast
      }
    })
  }else{
    wx.navigateTo({
      url: '/pages/index/index'
    })
  }
}